//
//  CustomMenuView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/17.
//

import UIKit
@objc protocol CustomMenuViewDelegate {
    func deleteTap(title:String)
}
class CustomMenuView: UIView {
    weak var delegate : CustomMenuViewDelegate?
    private var lastTrans : CGAffineTransform?
    var isHiddenClose:Bool{
        set{
            close.isHidden = canRemove ? newValue : true
            titleButton.setTitleColor(canRemove ? B000_COLOR:B999_TITLE_COLOR, for: .normal)
        }
        get{
            return close.isHidden
        }
    }

    let titleButton = UIButton()
    private lazy var close:UIButton={
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "close")?.reSizeImage(reSize: CGSize.init(width: 10, height: 10)), for: .normal)
        btn.addTarget(self, action: #selector(buttonTap), for: .touchUpInside)
        addSubview(btn)
        btn.snp.makeConstraints({
            $0.top.equalToSuperview()
            $0.right.equalToSuperview()
        })
        return btn
    }()
    private var canRemove:Bool = true
    init(title:String,canRemove:Bool = true) {
        super.init(frame: .zero)
        setRadiusAndDirection(radius: 5)
        backgroundColor = BACKGROUND_COLOR
        addSubview(titleButton)
        titleButton.font = Font_Size_14
        titleButton.setTitle(title, for: .normal)
        titleButton.setTitleColor(RED_COLOR, for: .selected)
        self.canRemove = canRemove
        titleButton.addTarget(self, action: #selector(buttonTap), for: .touchUpInside)
        titleButton.snp.makeConstraints({
            $0.center.equalToSuperview()
        })
        addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(buttonTap)))
        addGestureRecognizer(UILongPressGestureRecognizer.init(target: self, action: #selector(longPress(press:))))
        isHiddenClose = true
    }
    func angle2Radion(angle: Float) -> Float {
        return angle / Float(180.0 * Double.pi)
    }
    func startAnimation(){
        guard canRemove else {
            return
        }
        let animati = CAKeyframeAnimation(keyPath: "transform.rotation")
        // rotation 旋转，需要添加弧度值
        // 角度转弧度
        animati.values = [angle2Radion(angle: -50), angle2Radion(angle: 25), angle2Radion(angle: -25)]
        animati.repeatCount = Float(NSNotFound)
        self.layer.add(animati, forKey: "animati")
    }
    func stopAnimation() {
        guard canRemove else {
            return
        }
        self.layer.removeAnimation(forKey: "animati")
    }
    
    @objc func longPress(press:UILongPressGestureRecognizer){
        if press.state == .began {
            SystemLog("begin")
            self.lastTrans = self.transform
            self.transform = self.transform.scaledBy(x: 1.2, y: 1.2)
            NotificationMananger.post(.Custom(name: "EditBegin"))
        }else if press.state == .ended{
            SystemLog("end")
            self.transform = self.lastTrans!
        }
    }
    @objc func buttonTap(){
        if !isHiddenClose {
            guard canRemove else {
                return
            }
            delegate?.deleteTap(title: titleButton.currentTitle!)
        }else{
            SystemLog(titleButton.currentTitle)
        }
    }
    required init?(coder: NSCoder) {
        fatalError()
    }
}
